/**
 * 
 */
package org.pump.client.codec;

import org.apache.mina.core.buffer.IoBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author liudong03
 *
 */
public class PacketHead {
	public static final int HEADSIZE = 4;
	private static final Logger mylog = LoggerFactory.getLogger(PacketHead.class);

	int packetlen;
	short packetnum;
	
	public static PacketHead readbuffer(IoBuffer in)
	{
		int buflen = in.remaining();
		if (buflen < HEADSIZE) {
			return null;
		}
		PacketHead temphead = new PacketHead();
		temphead.packetlen = in.getUnsignedMediumInt();
		temphead.packetnum = in.getUnsigned();
		
		return temphead;
	}
	
	public boolean writebuffer(IoBuffer buf) {
		buf.putMediumInt(packetlen);
		buf.putUnsigned(packetnum);
		return true;
	}
	/**
	 * @return the packetlen
	 */
	public int getPacketlen() {
		return packetlen;
	}
	/**
	 * @param packetlen the packetlen to set
	 */
	public void setPacketlen(int packetlen) {
		this.packetlen = packetlen;
	}
	/**
	 * @return the packetnum
	 */
	public short getPacketnum() {
		return packetnum;
	}
	/**
	 * @param packetnum the packetnum to set
	 */
	public void setPacketnum(byte packetnum) {
		this.packetnum = (short) (packetnum & 0xff);
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "PacketHead [packetlen=" + packetlen + ", packetnum="
				+ packetnum + "]";
	}
}
